home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / utils / whati~gz.zoo / common.c < prev    next >
C/C++ Source or Header  |  1992-09-13  |  5KB  |  293 lines

  1. /*
  2.  *    common.c - stuff used by all programs
  3.  */
  4.  
  5. static char *rcsid_common_c = "$Id: common.c,v 2.0 1992/09/13 05:02:44 rosenkra Exp $";
  6. /*
  7.  * $Log: common.c,v $
  8.  * Revision 2.0  1992/09/13  05:02:44  rosenkra
  9.  * total rewrite. this if first rev of this file.
  10.  *
  11.  *
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17.  
  18. #include "whatis.h"
  19.  
  20. extern int    debugging;
  21.  
  22. /*------------------------------*/
  23. /*    parse_record        */
  24. /*------------------------------*/
  25.  
  26. /*
  27.  *    macros to make parsing easier
  28.  */
  29. #define EOS            '\0'
  30. #define NL            '\n'
  31. #define FIELDSEP        '%'
  32. #define ITEMSEP            ','
  33.  
  34. #define END_OF_STRING(c)    (c==EOS || c==NL)
  35. #define END_OF_FIELD(c)        (c==FIELDSEP)
  36. #define END_OF_ITEM(c)        (c==ITEMSEP)
  37.  
  38. #define ADVANCE_FIELD    \
  39.  while(!END_OF_STRING(*ps) && !END_OF_FIELD(*ps)){ps++;} \
  40.  if(END_OF_STRING(*ps)){*ps=EOS;return(lastone?0:1);} \
  41.  *ps++ = EOS;
  42.  
  43. #define ADVANCE_ITEM    \
  44.  while(!END_OF_STRING(*ps) && !END_OF_ITEM(*ps) && !END_OF_FIELD(*ps)){ps++;} \
  45.  if(END_OF_STRING(*ps)){*ps=EOS;return(lastone?0:1);} \
  46.  if(END_OF_FIELD(*ps)){*ps++ = EOS;break;} \
  47.  *ps++ = EOS;
  48.  
  49.  
  50. int parse_record (char *raw, struct rec *r)
  51. {
  52.  
  53. /*
  54.  *    parse record. caller provides raw record and space for parsed
  55.  *    structure. ret 0 if all ok, else 1 (incomplete record, etc).
  56.  *    raw input is a null terminated string (with/without newline).
  57.  */
  58.  
  59.     char   *ps = raw;
  60.     int    i;
  61.     int    lastone = 0;
  62.  
  63.  
  64.     /*
  65.      *   set up default (empty)
  66.      */
  67.     r->name     = NULL;
  68.     r->alias[0] = NULL;    r->alias[1] = NULL;
  69.     r->section  = NULL;
  70.     r->subsect  = NULL;
  71.     r->desc     = NULL;
  72.     r->xref[0]  = NULL;    r->xref[1]  = NULL;
  73.     r->keyw[0]  = NULL;    r->keyw[1]  = NULL;
  74.  
  75.  
  76.  
  77.     /*
  78.      *   read name (required!)
  79.      */
  80.     r->name = ps;
  81.     ADVANCE_FIELD;
  82.  
  83.  
  84.  
  85.     /*
  86.      *   read any aliases (optional)
  87.      */
  88.     if (ps[0] == '_' && (END_OF_FIELD(ps[1]) || END_OF_STRING(ps[1])))
  89.     {
  90.         r->alias[0] = NULL;
  91.         ADVANCE_FIELD;
  92.     }
  93.     else
  94.     {
  95.         for (i = 0; i < MAX_ALIAS-1; i++)
  96.         {
  97.             if (*ps == EOS)
  98.             {
  99.                 r->alias[i] = NULL;
  100.                 return (1);
  101.             }
  102.             r->alias[i] = ps;
  103.             r->alias[i+1] = NULL;
  104.             ADVANCE_ITEM;
  105.         }
  106.     }
  107.  
  108.  
  109.  
  110.     /*
  111.      *   read section (required!)
  112.      */
  113.     r->section = ps;
  114.     ADVANCE_FIELD;
  115.  
  116.  
  117.  
  118.     /*
  119.      *   read subsection (optional)
  120.      */
  121.     r->subsect = ps;
  122.     ADVANCE_FIELD;
  123.     if (r->subsect[0] == '_')
  124.         r->subsect = NULL;
  125.  
  126.  
  127.  
  128.     /*
  129.      *   read description (required)
  130.      */
  131.     r->desc = ps;
  132.     ADVANCE_FIELD;
  133.  
  134.  
  135.  
  136.     /*
  137.      *   read any xrefs (optional)
  138.      */
  139.     if (ps[0] == '_' && (END_OF_FIELD(ps[1]) || END_OF_STRING(ps[1])))
  140.     {
  141.         r->xref[0] = NULL;
  142.         ADVANCE_FIELD;
  143.     }
  144.     else
  145.     {
  146.         for (i = 0; i < MAX_XREF-1; i++)
  147.         {
  148.             if (*ps == EOS)
  149.             {
  150.                 r->xref[i] = NULL;
  151.                 return (1);
  152.             }
  153.             r->xref[i] = ps;
  154.             r->xref[i+1] = NULL;
  155.             ADVANCE_ITEM;
  156.         }
  157.     }
  158.  
  159.  
  160.  
  161.     /*
  162.      *   read any keywords (optional)
  163.      */
  164.     lastone = 1;
  165.     if (ps[0] == '_' && (END_OF_FIELD(ps[1]) || END_OF_STRING(ps[1])))
  166.     {
  167.         r->keyw[0] = NULL;
  168.         return (1);
  169.     }
  170.     else
  171.     {
  172.         for (i = 0; i < MAX_KEYW-1; i++)
  173.         {
  174.             if (*ps == EOS)
  175.             {
  176.                 r->keyw[i] = NULL;
  177.                 return (0);
  178.             }
  179.             r->keyw[i] = ps;
  180.             r->keyw[i+1] = NULL;
  181.             ADVANCE_ITEM;
  182.         }
  183.     }
  184.  
  185.  
  186.     return (0);
  187. }
  188.  
  189.  
  190.  
  191.  
  192. /*------------------------------*/
  193. /*    print_record        */
  194. /*------------------------------*/
  195. void print_record (int verbose, struct rec *r)
  196. {
  197.  
  198. /*
  199.  *    print record. one line, just:
  200.  *
  201.  *        name(sect[subsect]) - description.
  202.  *
  203.  *    if verbose, add aliases, see also, and keywords, if any
  204.  */
  205.  
  206.     int i;
  207.  
  208.  
  209.     if (r->name)
  210.         printf ("%s", r->name);
  211.  
  212.  
  213.     if (r->section)
  214.     {
  215.         printf ("(%s", r->section);
  216.         if (r->subsect)
  217.             printf ("%s", r->subsect);
  218.         printf (") -");
  219.     }
  220.     else
  221.         printf (" -");
  222.  
  223.  
  224.     if (r->desc)
  225.         printf (" %s\n", r->desc);
  226.     else
  227.         printf (" unknown (no description in database)\n");
  228.     
  229.  
  230.     if (!verbose)
  231.         return;
  232.  
  233.  
  234.     if (r->alias[0] != NULL)
  235.     {
  236.         printf ("\tprimary manpage:\n");
  237.         for (i = 0; r->alias[i] != NULL; i++)
  238.             printf ("\t\t%s\n", r->alias[i]);
  239.     }
  240.     
  241.     
  242.     if (r->xref[0] != NULL)
  243.     {
  244.         printf ("\tsee also:\n");
  245.         for (i = 0; r->xref[i] != NULL; i++)
  246.             printf ("\t\t%s\n", r->xref[i]);
  247.     }
  248.     
  249.     if (r->keyw[0] != NULL)
  250.     {
  251.         printf ("\tkeywords:\n");
  252.         for (i = 0; r->keyw[i] != NULL; i++)
  253.             printf ("\t\t%s\n", r->keyw[i]);
  254.     }
  255.  
  256.     printf ("\n");
  257.  
  258.     return;
  259. }
  260.  
  261.  
  262.  
  263.  
  264. #ifdef CHECK_MAGIC
  265.  
  266. /*------------------------------*/
  267. /*    check_magic        */
  268. /*------------------------------*/
  269. int check_magic (void)
  270. {
  271.  
  272. /*
  273.  *    check magic "number" of file. if ok, return 0. else 1.
  274.  *    should be first line of file. check only MAGIC_CHECK_LEN bytes.
  275.  */
  276.  
  277.     char    buf[REC_SIZE];
  278.  
  279.     fgets (buf, REC_SIZE-1, stdin);
  280.     if (feof (stdin))
  281.         return (1);
  282.     if (debugging)
  283.         fprintf (stderr, "magic: %s", buf);
  284.  
  285.     if (!strncmp (buf, MAGIC, MAGIC_CHECK_LEN))
  286.         return (0);
  287.  
  288.     return (1);
  289. }
  290. #endif /*CHECK_MAGIC*/
  291.  
  292.  
  293.